/***************************************************************************
 *   This file is a part of match_mail.                               *
 *   Copyright (C) 2009 by fu4ny 										   *
 *   ntcong.it@gmail.com                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>   *             *
 ***************************************************************************/

#include "Logger.h"
#include <cstdarg>
#include <ctime>

Logger::Logger() {
	isHtml = false;
	strcpy(logFile,"match_mail.log");
	newFile(logFile);
}

Logger::Logger( const char* ilogFile ) {
	isHtml = false;
	newFile( ilogFile );
	strcpy(logFile, ilogFile);
}

Logger::Logger( const string ilogFile ) {
	isHtml = false;
	newFile( ilogFile.c_str() );
	strcpy(logFile, ilogFile.c_str());
}

Logger::~Logger() {
	log.open( logFile, ios::app);
	log << "</body>";
	log << "</html>";
	log.close();
}

void Logger::newFile( const char* logFile ) {
	log.open( logFile );
	log.close();
}

string Logger::lightbox() const {
	ostringstream oss;
	oss << "<link rel=\"stylesheet\" href=\"css/lightbox.css\" type=\"text/css\" media=\"screen\" />\n"
		<< "<script type=\"text/javascript\" src=\"js/prototype.js\"></script>\n"
		 << "<script type=\"text/javascript\" src=\"js/scriptaculous.js?load=effects,builder\"></script>\n"
		 << "<script type=\"text/javascript\" src=\"js/lightbox.js\"></script>\n";
	return oss.str();
}

void Logger::setHtml() {
	isHtml = true;
	log.open( logFile, ios::app);
	log << "<html>\n";
	log << "<head>" << lightbox()
         <<     "</head>\n";
	log << "<body>";
	log.close();
}

string Logger::endLine() {
	if ( isHtml ) return "<br />\n";
	else return "\n";
}

string Logger::currentTime() {
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	ostringstream oss;
	oss << "[" << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << timeinfo->tm_sec << "]";
	return oss.str();
}

void Logger::write() {
	log.open( logFile, ios::app);
	log << currentTime() << oss.str() << endLine();
	log.close();
	oss.str("");
}
